package com.example.demo2.controller;


import com.example.demo2.entity.Course;
import com.example.demo2.service.CourseService;
import com.google.gson.Gson;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/lv_course")
public class CourseController {



//    course_id, course_name, user_id, graduation_point
    @Autowired
   private CourseService courseService;
    //要注入对应的接口实现类，首先应该保证容器中有这样的类

//    增加一门课程
    @RequestMapping("/addCourse")
    public int addCourse(@Param("course_id") Integer courseId,@Param("course_name") String courseName,@Param("user_id") Integer userId,
                          @Param("graduation_requirments") String graduationPoint){

        Course course=new Course();
        course.setCourseId(courseId);
        course.setCourseName(courseName);
        course.setUserId(userId);
        course.setGraduationPoint(graduationPoint);
        course.setHaveTarget(0);

//graduationPoint

        return courseService.addCourse(course);




    }


//    删除课程
    @RequestMapping("/DeleteCourse")
    public void DeleteCourse(@Param("course_id") Integer courseId)
    {
        courseService.Lv_deleteCourseById(courseId);
    }


//    根据课程ID来搜索
    @RequestMapping("/SelectCourseById")
    public Course SelectCourseById(@Param("course_id")Integer courseId)
    {
        return courseService.Lv_selectCourseById(courseId);
    }

//根据用户ID，显示该用户的所有课程
    @RequestMapping("/showAllCourse")
//    要不要给 Integer userId 作为参数
    public String showAllCourse()
    {
        List<Course> list=courseService.Lv_showAllCourse();

        Gson gson = new Gson();
        return gson.toJson(list);
    }

//    更新课程  看看是否可以
    @RequestMapping("/updateCourseById")
    public int updateCourseById(@Param("course_id") Integer courseId,@Param("course_name") String courseName,@Param("user_id") Integer userId,
                                 @Param("graduation_point") String graduationPoint)
    {
        Course course=new Course();
        course.setCourseId(courseId);
        course.setCourseName(courseName);
        course.setUserId(userId);
        course.setGraduationPoint(graduationPoint);
         return courseService.Lv_updateCourseById(course);
    }


//       标记课程具有课程目标
        @RequestMapping("/updateHaveTarget")
        public int updateHaveTarget(@Param("course_id") Integer courseId)
        {
            return courseService.Lv_updateHaveTarget(courseId);
        }

    @GetMapping("/findNameById")
    public String findNameById(Integer courseId){
        Course byId = courseService.findNameById(courseId);
        Gson gson = new Gson();
        return gson.toJson(byId);
    }



}

